﻿/// <reference name="MicrosoftAjax.js"/>
/// <referecn name="ListItem.js" />

Type.registerNamespace("AGuang");

AGuang.ItemSelect = function(element) {
    AGuang.ItemSelect.initializeBase(this, [element]);
    
    this._targetControlID=null;
    this._position=null;
    this._x=1;
    this._y=1;
    this._width=null;
    this._height=null;
    this._cssClass=null;
    this._style=null;
    
    this._itemWidth="100%";
    this._itemHeight="20px";
    this._itemForeColor="#000000";
    this._itemBackColor="#FFFFFF";
    this._itemOverForeColor="#FFFFFF";
    this._itemOverBackColor="#3333DD";
    this._itemStyle=null;
    this._itemCssClass=null;
    this._itemOverStyle=null;
    this._itemOverCssClass=null;
    
    this._container=null;
    this._items=[];
    this._itemList={};
    this._preSelectNum=-1;
    
    this._item$Events={
        mouseover:Function.createDelegate(this,this._itemOver),
        mouseout:Function.createDelegate(this,this._itemOut),
        click:Function.createDelegate(this,this._itemClick)
    };
    
    this._document$Events$Delegate=Function.createDelegate(this,this._keyPress);
    this._isAddKeyEvent=false;//是否加添加键盘按下事件
}

AGuang.ItemSelect.prototype = {
    get_targetControlID:function(){
        return this._targetControlID;
    },
    set_targetControlID:function(value){
        this._targetControlID=value;
    },
    
    get_position:function(){
        return this._position;
    },
    set_position:function(value){
        this._position=value;
    },
    
    get_x:function(){
        return this._x;
    },
    set_x:function(value){
        if(!isNaN(value)){
            value+="px";
        }
        this._x=value;
        if(this._container){
            this._container.style.left=this._x;
        }
    },
    get_y:function(){
        return this._y;
    },
    set_y:function(value){
        if(!isNaN(value)){
            value+="px";
        }
        this._y=value;
        if(this._container){
            this._container.style.top=this._y;
        }
    },
    
    get_width:function(){
        return this._width;
    },
    set_width:function(value){
        if(!isNaN(value)){
            value+="px";
        }
        this._width=value;
        if(this._container){
            this._container.style.width=this._width;
        }
    },
    get_height:function(){
        return this._height;
    },
    set_height:function(value){
        if(!isNaN(value)){
            value+="px";
        }
        this._height=value;
        if(this._container){
            this._container.style.height=this._height;
        }

    },
    set_cssClass:function(value){
        this._cssClass=value;
        if(this._container){
            this._container.className=this._cssClass;
        }
    },
    get_cssClass:function(){
        return this._cssClass;
    },
    set_style:function(value){
        this._style=value;
        if(this._container){
            this._container.style.cssText=this._joinStyle(this._container.style.cssText,this._style);
        }

    },
    get_style:function(){
        return this._style;
    },
    set_itemWidth:function(value){
        if(!isNaN(value)){
            value+="px";
        }
        this._itemWidth=value;
        
    },
    get_itemWidth:function(){
        return this._itemWidth;
    },
    set_itemHeight:function(value){
        if(!isNaN(value)){
            value+="px";
        }
        this._itemHeight=value;
    },
    get_itemHeight:function(){
        return this._itemHeight;
    },
    set_itemForeColor:function(value){
        this._itemForeColor=value;
    },
    get_itemForeColor:function(){
        return this._itemForeColor;
    },
    set_itemBackColor:function(value){
        this._itemBackColor=value;
    },
    get_itemBackColor:function(){
        return this._itemBackColor;
    },
    set_itemOverForeColor:function(value){
        this._itemOverForeColor=value;
    },
    get_itemOverForeColor:function(){
        return this._itemOverForeColor;
    },
    set_itemOverBackColor:function(value){
        this._itemOverBackColor=value;
    },
    get_itemOverBackColor:function(){
        return this._itemOerBackColor;
    },
    set_itemStyle:function(value){
        this._itemStyle=value;
    },
    get_itemStyle:function(){
        return this._itemStyle;
    },
    set_itemCssClass:function(value){
        this._itemCssClass=value;
    },
    get_itemCssClass:function(){
        return this._itemCssClass;
    },
    set_itemOverStyle:function(value){
        this._itemOverStyle=value;
    },
    get_itemOverStyle:function(){
        return this._itemOverStyle;
    },
    set_itemOverCssClass:function(value){
        this._itemOverCssClass=value;
    },
    get_itemOverCssClass:function(){
        return this._itemOverCssClass;
    },
    add_showing:function(handler){
        this.get_events().addHandler("showing",handler)
    },
    remove_showing:function(handler){
        this.get_events().removeHandler("showing",handler);
    },
    add_shown:function(handler){
        this.get_events().addHandler("shown",handler);
    },
    remove_shown:function(handler){
        this.get_events().removeHandler("shown",handler);
    },
    add_selected:function(handler){
        this.get_events().addHandler("selected",handler);
    },
    remove_selected:function(handler){
        this.get_events().removeHandler("selected",handler);
    },
    initialize: function() {
        AGuang.ItemSelect.callBaseMethod(this, 'initialize');
        
        //在此处添加自定义初始化
    },
    dispose: function() {        
        //在此处添加自定义释放操作
        AGuang.ItemSelect.callBaseMethod(this, 'dispose');
    },
    _create:function(){
        //创建框架
        this._container=document.createElement("DIV");
        this._container.style.position="absolute";
        this._container.style.background="#FFF";
        this._container.style.border="1px solid #666";
        this._container.style.padding="4px 5px";
        this._container.style.fontSize="12px";
        this._container.style.overflow="hidden";
        this._container.style.width=this._width;
        this._container.style.height=this._height;
        if(!this._targetControlID){
            this._container.style.left=this._x;
            this._container.style.top=this._y;
        }
        this._container.style.cssText=this._joinStyle(this._container.style.cssText+"",this._style+"");
        this._container.className=this._cssClass;
        this.get_element().appendChild(this._container);
    },
    _createItem:function(item){
        //创建每一个项目
        var itemControl=document.createElement("DIV");
        itemControl.mark="item";
        itemControl.style.width=this._itemWidth;
        itemControl.style.height=this._itemHeight;
        itemControl.style.lineHeight=this._itemHeight;
        itemControl.style.backgroundColor=this._itemBackColor;
        itemControl.style.color=this._itemForeColor;
        itemControl.style.cssText=this._joinStyle(itemControl.style.cssText,this._itemStyle);
        itemControl.className=this._itemCssClass;
        //itemControl.removeChild
        
        
        //显示图标
        if(item.get_icon()){
            var icon=new Image();
            icon.src=item.get_icon();
            icon.align="absmiddle";
            itemControl.appendChild(icon);
            itemControl._icon=icon;
        }
        //显示值
        var textNode=document.createTextNode(item.get_text());
        itemControl.appendChild(textNode);
        itemControl._textNode=textNode;
        
        //实际值
        itemControl.itemValue=item;
        this._itemList[item.get_value()]=itemControl;
        this._container.appendChild(itemControl);
        $addHandlers(itemControl,this._item$Events);
    },
    _deleteItem:function(item){
        //删除某个项目
        var itemControl=this._itemList[item];
        if(!itemControl) return;
        $clearHandlers(itemControl);
        this._container.removeChild(itemControl);
    },
    get_text:function(){
        //取得所选的值
        var len=this.get_itemCount();
        for(var i=0;i<len;i++){
            var item=this._items[i];
            if(item.get_selected()){
                return item.get_value();
            }
        }
        return null;
    },
    show:function(){
         //显示
         if(!this._container){
            this._create();
            for(var i=0;i<this._items.length;i++){
                this._createItem(this._items[i]);
            }
         }
         this._container.style.display="";
         if(!this._isAddKeyEvent){
            $addHandler(document,"keydown",this._document$Events$Delegate);
            this._isAddKeyEvent=true;
         }
    },
    hide:function(){
        //隐藏
        if(this._container) this._container.style.display="none";
        if(this._isAddKeyEvent){
            $removeHandler(document,"keydown",this._document$Events$Delegate);
            this._isAddKeyEvent=false;
        }
    },
    addItem:function(text,value,icon){
        //添加项目
        var item=new AGuang.ListItem();
        item.set_text(text);
        if(value) item.set_value(value);
        if(icon) item.set_icon(icon);
        this._items.push(item);
        
        if(this._container){
            this._createItem(item);    
        }
    },
    removeItem:function(num){
        //删除项目
        if(num>=this._items.length) return;
        this._deleteItem(this._items[num].get_value());
        Array.removeAt(this._items,num);
    },
    removeItemByText:function(text){
        //删除项目
    },
    changeItem:function(num,text,value,icon){
        //改变某个项目的名称
        if(num>=this._items.length) return;
        var item=this._items[num];
        var itemControl=this._itemList[item.get_value()];
        //alert(itemControl._textNode.nodeValue);
        item.set_text(text);
        if(value) item.set_value(value);
        else item.set_value(text);
        if(icon) item.set_icon(icon);
        
        
        if(this._container){
            //显示图标
            if(item.get_icon()&&itemControl._icon){
                var icon=itemControl._icon;
                icon.src=item.get_icon();
            }
            //显示值
            var textNode=itemControl._textNode;
            textNode.nodeValue=text;
        }
    },
    clearItems:function(){
        //清除所有项目
        var len=this.get_itemCount();
        for(var i=len-1;i>=0;i--){
            this.removeItem(i);
        }
    },
    get_itemCount:function(){
        //取得项目的个数
        return this._items.length;
    },
    _joinStyle:function(style1,style2){
            if(style1==null) style1="";
            if(style2==null) style2="";
            style1=style1.trim();
            style2=style2.trim();
            
            if(style1.length>0){
                if(style1.lastIndexOf(";")==style1.length-1){
                    return style1+style2;
                }else{
                    return style1+";"+style2;
                }
            }
            return style2;
    },
    _preSelect:function(num){
        if(num>=this._items.length) return;
        this._clearPreSelect();
        var item=this._items[num];
        var itemControl=this._itemList[item.get_value()];
        
        itemControl.style.backgroundColor=this._itemOverBackColor;
        itemControl.style.color=this._itemOverForeColor;
        itemControl.style.cssText=this._joinStyle(itemControl.style.cssText,this._itemOverStyle);
        itemControl.className=this._itemOverCssClass;
        this._preSelectNum=num;
    },
    _clearPreSelect:function(){
      var len=this._items.length;
      for(var i=0;i<len;i++){
        var item=this._items[i];
        var itemControl=this._itemList[item.get_value()];
        itemControl.style.backgroundColor=this._itemBackColor;
        itemControl.style.color=this._itemForeColor;
        itemControl.style.cssText=this._joinStyle(itemControl.style.cssText,this._itemStyle);
        itemControl.className=this._itemCssClass;
      }  
    },
    select:function(num){
        for(var i=0;i<this._items.length;i++){
            if(i==num){
                this._items[i].set_selected(true);
            }else{
                this._items[i].set_selected(false);
            }
        }
        this._preSelectNum=num;
    },
    _itemOver:function(e){
        //鼠标经过事件
        e.stopPropagation();
        var target=e.target;
        if(target.itemValue){
            for(var i=0;i<this._items.length;i++){
                if(this._items[i]==target.itemValue){
                    this._preSelect(i);
                    return false;
                }
            }
        }

    },
    _itemOut:function(e){
        //鼠标经出事件
        e.stopPropagation();
    },
    _itemClick:function(e){
        //鼠标按下
        e.stopPropagation();
        var target=e.target;
        if(target.itemValue){
            for(var i=0;i<this._items.length;i++){
                if(this._items[i]==target.itemValue){
                    this.select(i);
                    var aHandler=this.get_events().getHandler("selected");
                    if(aHandler) aHandler(this,Sys.EventArgs.Empty);
                    return false;
                }
            }
        }
    },
    _keyPress:function(e){
        switch(e.keyCode){
            case Sys.UI.Key.down:
                this._preSelectNum++;
                var aac=this._preSelectNum;
                if(this._preSelectNum>=this._items.length) this._preSelectNum=0;
                this._preSelect(this._preSelectNum);
                e.stopPropagation();     
                e.preventDefault();     
                return false; 
                break;
            case Sys.UI.Key.up:
                this._preSelectNum--;
                if(this._preSelectNum<=-1) this._preSelectNum=this._items.length-1;
                this._preSelect(this._preSelectNum);
                 e.stopPropagation();     
                e.preventDefault();     
               return false; 
                break;
            case Sys.UI.Key.enter:
                this.select(this._preSelectNum);
                var aHandler=this.get_events().getHandler("selected");
                if(aHandler) aHandler(this,Sys.EventArgs.Empty);
                e.stopPropagation();     
                e.preventDefault();     
                return false; 
                break;
            case Sys.UI.Key.tab:
                this.select(this._preSelectNum);
                var aHandler=this.get_events().getHandler("selected");
                if(aHandler) aHandler(this,Sys.EventArgs.Empty);
                break;
        }
    }
}
AGuang.ItemSelect.registerClass('AGuang.ItemSelect', Sys.UI.Control);
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
